package com.ruoyi.distributionGrade.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.distributionGrade.mapper.DistributionGradeMapper;
import com.ruoyi.distributionGrade.domain.DistributionGrade;
import com.ruoyi.distributionGrade.service.IDistributionGradeService;

/**
 * 分销等级Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-11
 */
@Service
public class DistributionGradeServiceImpl implements IDistributionGradeService 
{
    @Autowired
    private DistributionGradeMapper distributionGradeMapper;

    /**
     * 查询分销等级
     * 
     * @param gradeId 分销等级主键
     * @return 分销等级
     */
    @Override
    public DistributionGrade selectDistributionGradeByGradeId(Long gradeId)
    {
        return distributionGradeMapper.selectDistributionGradeByGradeId(gradeId);
    }

    /**
     * 查询分销等级列表
     * 
     * @param distributionGrade 分销等级
     * @return 分销等级
     */
    @Override
    public List<DistributionGrade> selectDistributionGradeList(DistributionGrade distributionGrade)
    {
        return distributionGradeMapper.selectDistributionGradeList(distributionGrade);
    }

    /**
     * 新增分销等级
     * 
     * @param distributionGrade 分销等级
     * @return 结果
     */
    @Override
    public int insertDistributionGrade(DistributionGrade distributionGrade)
    {
        distributionGrade.setCreateTime(DateUtils.getNowDate());
        return distributionGradeMapper.insertDistributionGrade(distributionGrade);
    }

    /**
     * 修改分销等级
     * 
     * @param distributionGrade 分销等级
     * @return 结果
     */
    @Override
    public int updateDistributionGrade(DistributionGrade distributionGrade)
    {
        distributionGrade.setUpdateTime(DateUtils.getNowDate());
        return distributionGradeMapper.updateDistributionGrade(distributionGrade);
    }

    /**
     * 批量删除分销等级
     * 
     * @param gradeIds 需要删除的分销等级主键
     * @return 结果
     */
    @Override
    public int deleteDistributionGradeByGradeIds(Long[] gradeIds)
    {
        return distributionGradeMapper.deleteDistributionGradeByGradeIds(gradeIds);
    }

    /**
     * 删除分销等级信息
     * 
     * @param gradeId 分销等级主键
     * @return 结果
     */
    @Override
    public int deleteDistributionGradeByGradeId(Long gradeId)
    {
        return distributionGradeMapper.deleteDistributionGradeByGradeId(gradeId);
    }

    /**
     * 查询下一级分销等级
     *
     * @param currentGradeId 当前等级id
     * @return 下级分销等级
     */
    @Override
    public DistributionGrade selectNextGrade(Long currentGradeId) {
        // 1. 获取当前等级
        DistributionGrade  currentGrade = distributionGradeMapper.selectDistributionGradeByGradeId(currentGradeId);
        if (currentGrade == null) {
            return null;
        }
        // 2. 查询比当前等级权重高的最接近的等级
        DistributionGrade param = new DistributionGrade();
        param.setGradeWeight(currentGrade.getGradeWeight());
        List<DistributionGrade> higherGrades = distributionGradeMapper.selectHigherGrades(param);

        if (higherGrades != null && !higherGrades.isEmpty()) {
            // 返回权重最小的那个（最接近当前等级的下一级）
            return higherGrades.get(0);
        }

        return null;
    }

    /**
     * 获取默认分销等级
     *
     * @return 默认分销等级
     */
    @Override
    public DistributionGrade selectDefaultGrade() {
        return distributionGradeMapper.selectDefaultGrade();
    }
}
